﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EarringsBackEnd
{
    public partial class frmEarringsConfigs : Form
    {
        private DataTable _dataTable = null;
        private BindingSource _bindingSource = null;
        private Boolean _flagEdit = false;
        private ConfigType _eConfType;
        private bool _chkEmptyValue = true;
        private string[] _strArrHeader = null;
        private string _typeName = string.Empty;
        private string _typeNameTh = string.Empty;
        private string _shrtName = string.Empty;
        private string _grpTypeName = string.Empty;
        private MasterDataManagement _dtMgmt = null;

        public frmEarringsConfigs()
        {
            InitializeComponent();
        }

        private void frmEarringsStyles_Load(object sender, EventArgs e)
        {
            this.InitializeVariableValue();
            this.GetData();
            this.InitializeDataGridView();
            this.InitializeLabel();
        }

        public void FromMenu(ConfigType confType)
        {
            _eConfType = confType;
        }

        private void dtgvERConfig_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int rowIndex = e.RowIndex;
            DataGridViewRow row = dtgvERConfig.Rows[rowIndex];

            int colIndex = e.ColumnIndex;
            DataGridViewColumn col = dtgvERConfig.Columns[colIndex];

            //MessageBox.Show(col.Name.ToString());

            if (col.Name.ToString() == "แก้ไข" && !string.IsNullOrEmpty(row.Cells[3].Value.ToString()))
            {
                //แก้ไขข้อมูล
                _flagEdit = true;

                txtKeyId.Text = row.Cells[3].Value.ToString();
                txtName.Text = row.Cells[5].Value.ToString();
                txtNameTh.Text = row.Cells[4].Value.ToString();
                txtShortName.Text = row.Cells[6].Value.ToString();
                //txtShortName.ReadOnly = false;
                //txtShortName.Enabled = false;
                btnAddNew.Text = "บันทึก";

            }
            else if (col.Name.ToString() == "ลบ" && !string.IsNullOrEmpty(row.Cells[2].Value.ToString()))
            {
                //ลบข้อมูล
                string delKey = row.Cells[3].Value.ToString();
                if (delKey == txtKeyId.Text)
                {
                    DialogResult result = MessageBox.Show("คุณต้องการลบข้อมูลรูปแบบต่างหู รหัส: " + delKey + " หรือไม่?", "ยืนยัน", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        this.DeleteData(delKey);
                    }
                    else if (result == DialogResult.No)
                    {
                        dtgvERConfig.ClearSelection();
                    }
                }
                else
                {
                    this.DeleteData(delKey);
                }

                //Refresh Grid
                this.GetData();
                this.ClearValue();
            }
        }

        private void dtgvERConfig_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            
            //DataGridViewCellStyle style = dtgvERConfig.ColumnHeadersDefaultCellStyle;
            //style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            //style.BackColor = Color.Navy;
            //style.ForeColor = Color.White;


            //if (dtgvERConfig.Columns[e.ColumnIndex].Name.ToString() == "NameTH")
            //{
            //    e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            //}

            //if (dtgvERConfig.Columns[e.ColumnIndex].Name.ToString() == "NameEN")
            //{
            //    e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            //}

            //if (dtgvERConfig.Columns[e.ColumnIndex].Name.ToString() == "ShortName")
            //{
            //    e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            //}

        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            this.CheckEmptyValue();

            if(!_chkEmptyValue)
            { 
                if (!_flagEdit)
                {
                    //Save
                    this.SaveData();
                }
                else
                {
                    //Update 
                    this.UpdateData();
                    _flagEdit = false;
                }

                //Refresh Grid
                this.GetData();
                this.ClearValue();
                MessageBox.Show("บันทึกข้อมูลเรียบร้อย");
            }
            else
            {
                MessageBox.Show("กรุณกรอกข้อมูลให้ครบถ้วน");
            }
        }


        private void btnClear_Click(object sender, EventArgs e)
        {
            this.ClearValue();
            _flagEdit = false;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.ClearValue();
            _flagEdit = false;
        }

        private void SaveData()
        {
            SqlParameter[] sqlParams = new SqlParameter[] 
                {
                    new SqlParameter("@Type", _typeName),
                    new SqlParameter("@TypeTh", _typeNameTh),
                    new SqlParameter("@Name", txtName.Text),
                    new SqlParameter("@NameTh", txtNameTh.Text),
                    new SqlParameter("@GroupT", _grpTypeName),
                    new SqlParameter("@ShortN", txtShortName.Text),
                    new SqlParameter("@ShortT", _shrtName),
                     new SqlParameter("@Remark", txtRemark.Text),
                };
            _dtMgmt = MasterDataManagement.InstanceCreation();
            _dtMgmt.SaveMasterData(ConfigType.EarringsStyles, sqlParams);
        }

        private void UpdateData()
        {
            SqlParameter[] sqlParams = new SqlParameter[] 
                {
                    new SqlParameter("@KeyId", txtKeyId.Text) ,
                    new SqlParameter("@Type", _typeName),
                    new SqlParameter("@Name", txtName.Text),
                    new SqlParameter("@NameTh", txtNameTh.Text),
                    new SqlParameter("@Value", txtName.Text)                    
                };
            _dtMgmt = MasterDataManagement.InstanceCreation();
            _dtMgmt.UpdateMasterData(ConfigType.EarringsStyles, sqlParams);
        }

        private void GetData()
        {
            SqlParameter[] sqlParams = new SqlParameter[] 
                {
                    new SqlParameter("@Type", _typeName) ,
                };
            _dtMgmt = MasterDataManagement.InstanceCreation();
            _dataTable = new DataTable();            
            _dataTable = _dtMgmt.GetMasterData(sqlParams);
            _bindingSource = new BindingSource();
            _bindingSource.DataSource = _dataTable;
            _bindingSource.ResetBindings(true);
            dtgvERConfig.DataSource = _bindingSource;
            dtgvERConfig.Refresh();                       
        }

        private void DeleteData(string delKey)
        {
            SqlParameter[] sqlParams = new SqlParameter[] 
                {
                    new SqlParameter("@KeyId", delKey)
                };
            _dtMgmt = MasterDataManagement.InstanceCreation();
            _dtMgmt.DeleteMasterData(sqlParams);
        }

        private void ClearValue()
        {
            foreach (Control ctl in tblLayout.Controls)
            {
                TextBox tb = ctl as TextBox;

                if (tb != null)
                {
                    tb.Clear();
                }
            }
            //txtShortName.ReadOnly = false;
            //txtShortName.Enabled = true;
        }

        private void CheckEmptyValue()
        {
            if (string.IsNullOrEmpty(txtName.Text) || string.IsNullOrEmpty(txtNameTh.Text) || txtShortName.TextLength < 2)
            {
                _chkEmptyValue = true;                
            }
            else
            {
                _chkEmptyValue = false;
            }
        }

        private void InitializeDataGridView()
        {
            //Set Header Grid
            for (int intIndex = 0; intIndex < dtgvERConfig.Columns.Count; intIndex++)
            {
                dtgvERConfig.Columns[intIndex].HeaderText = _strArrHeader[intIndex];
            }

            DataGridViewImageColumn modiCol = new DataGridViewImageColumn();
            modiCol.DataPropertyName = "Modify";
            modiCol.Width = 50;
            modiCol.HeaderText = "แก้ไข";
            modiCol.Image = EarringsBackEnd.Properties.Resources.Edit;
            modiCol.ImageLayout = DataGridViewImageCellLayout.Normal;
            dtgvERConfig.Columns.Insert(dtgvERConfig.Columns.Count , modiCol);

            DataGridViewImageColumn delCol = new DataGridViewImageColumn();
            delCol.DataPropertyName = "Del";
            delCol.Width = 50;
            delCol.HeaderText = "ลบ";
            delCol.Image = EarringsBackEnd.Properties.Resources.Del;
            delCol.ImageLayout = DataGridViewImageCellLayout.Normal;
            dtgvERConfig.Columns.Insert(dtgvERConfig.Columns.Count, delCol);

            this.AdjustColumnOrder();

            dtgvERConfig.Columns[0].Width = 70;
            dtgvERConfig.Columns[1].Width = 120;
            //dtgvERConfig.Columns[1].Visible  = false;
            dtgvERConfig.Columns[2].Width = 150;
            dtgvERConfig.Columns[3].Width = 170;
            dtgvERConfig.Columns[4].Width = 70;

            //DataGridViewCellStyle style = dtgvERConfig.ColumnHeadersDefaultCellStyle;
            //style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            //style.BackColor = Color.Navy;
            //style.ForeColor = Color.White;
            //style.Font = new Font(dtgvERConfig.Font, FontStyle.Regular);
        }

        private void AdjustColumnOrder()
        {
            //Set Column Name
            if(dtgvERConfig.Columns.Count == 7)
            {
                dtgvERConfig.Columns[0].Name = "No";
                dtgvERConfig.Columns[1].Name = "Id";
                dtgvERConfig.Columns[2].Name = "NameTH";
                dtgvERConfig.Columns[3].Name = "NameEN";
                dtgvERConfig.Columns[4].Name = "ShortName";
                dtgvERConfig.Columns[5].Name = "Edit";
                dtgvERConfig.Columns[6].Name = "Del";
           

                dtgvERConfig.Columns["No"].DisplayIndex = 0;
                dtgvERConfig.Columns["Id"].DisplayIndex = 1;
                dtgvERConfig.Columns["NameTH"].DisplayIndex = 2;
                dtgvERConfig.Columns["NameEN"].DisplayIndex = 3;
                dtgvERConfig.Columns["ShortName"].DisplayIndex = 4;
                dtgvERConfig.Columns["Edit"].DisplayIndex = 5;
                dtgvERConfig.Columns["Del"].DisplayIndex = 6;
            }
        }

        private void InitializeLabel()
        {
            label1.Text = _strArrHeader[1].ToString();
            label2.Text = _strArrHeader[2].ToString();
            label3.Text = _strArrHeader[3].ToString();
            label4.Text = _strArrHeader[4].ToString();
        }

        private void InitializeVariableValue()
        {
            switch (_eConfType)
            {
                case ConfigType.EarringsStyles://รูปแบบต่างหู
                    _strArrHeader = GlobalValue.gbStrArrStyHeader;
                    _typeName = GlobalValue.ERSTY_EN;
                    _typeNameTh = GlobalValue.ERSTY_EN;
                    _shrtName = GlobalValue.SHRTYP_ERSTY;
                    _grpTypeName = GlobalValue.GRPNAME_EARR;
                    break;
                case ConfigType.EarringsShape://รูปทรงต่างหู
                    _strArrHeader = GlobalValue.gbStrArrShpHeader;
                    _typeName = GlobalValue.ERSHP_EN;
                    _typeNameTh = GlobalValue.ERSHP_TH;
                    _shrtName = GlobalValue.SHRTYP_ERSHP;
                    _grpTypeName = GlobalValue.GRPNAME_EARR;
                    break;
                case ConfigType.EarringsBack://แป้นหลังต่างหู
                    _strArrHeader = GlobalValue.gbStrArrErBHeader;
                    _typeName = GlobalValue.ERBAC_EN;
                    _typeNameTh = GlobalValue.ERBAC_TH;
                    _shrtName = GlobalValue.SHRTYP_ERBAC;
                    _grpTypeName = GlobalValue.GRPNAME_OTHE;
                    break;
                case ConfigType.JewelryColor://สีอัญมณี
                    _strArrHeader = GlobalValue.gbStrArrJelHeader;
                    _typeName = GlobalValue.JWCOR_EN;
                    _typeNameTh = GlobalValue.JWCOR_TH;
                    _shrtName = GlobalValue.SHRTYP_JWCOR;
                    _grpTypeName = GlobalValue.GRPNAME_EARR;
                    break;
                case ConfigType.FaceShape://ลักษณะใบหน้า
                    _strArrHeader = GlobalValue.gbStrArrFSHHeader;
                    _typeName = GlobalValue.FASHP_EN;
                    _typeNameTh = GlobalValue.FASHP_TH;
                    _shrtName = GlobalValue.SHRTYP_FASHP;
                    _grpTypeName = GlobalValue.GRPNAME_PERS;
                    break;
                case ConfigType.EarShape://ลักษณะใบหู
                     _strArrHeader = GlobalValue.gbStrArrEARHeader;
                    _typeName = GlobalValue.EASHP_EN;
                    _typeNameTh = GlobalValue.EASHP_TH;
                    _shrtName = GlobalValue.SHRTYP_EASHP;
                    _grpTypeName = GlobalValue.GRPNAME_PERS;
                    break;
                case ConfigType.EarLobe://ลักษณะติ่งหู
                     _strArrHeader = GlobalValue.gbStrArrERLHeader;
                    _typeName = GlobalValue.ELSHP_EN;
                    _typeNameTh = GlobalValue.ELSHP_TH;
                    _shrtName = GlobalValue.SHRTYP_ELSHP;
                    _grpTypeName = GlobalValue.GRPNAME_PERS;
                    break;
                case ConfigType.NeckShape://ลักษณะลำคอ
                     _strArrHeader = GlobalValue.gbStrArrNECHeader;
                    _typeName = GlobalValue.NECK_EN;
                    _typeNameTh = GlobalValue.NECK_TH;
                    _shrtName = GlobalValue.SHRTYP_NECK;
                    _grpTypeName = GlobalValue.GRPNAME_PERS;
                    break;
                case ConfigType.HairStyles://ความยาวทรงผม
                     _strArrHeader = GlobalValue.gbStrArrHSTHeader;
                    _typeName = GlobalValue.HRSTY_EN;
                    _typeNameTh = GlobalValue.HRSTY_TH;
                    _shrtName = GlobalValue.SHRTYP_HRSTY;
                    _grpTypeName = GlobalValue.GRPNAME_PERS;
                    break;
                case ConfigType.SkinColor://สีผิว
                    _strArrHeader = GlobalValue.gbStrArrSKCHeader;
                    _typeName = GlobalValue.SKCOR_EN;
                    _typeNameTh = GlobalValue.SKCOR_TH;
                    _shrtName = GlobalValue.SHRTYP_SKCOR;
                    _grpTypeName = GlobalValue.GRPNAME_PERS;
                    break;
                case ConfigType.JawShape :
                    _strArrHeader = GlobalValue.gbStrArrJAWHeader;
                     _typeName = GlobalValue.JASHP_EN;
                     _typeNameTh = GlobalValue.JASHP_TH;
                    _shrtName = GlobalValue.SHRTYP_JASHP;
                    _grpTypeName = GlobalValue.GRPNAME_OTHE;
                    break;
                default:
                    break;
            }
        }
    }
}
